package com.yh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.model.Member;
import com.yh.model.Orders;
import com.yh.model.Product;
import com.yh.model.Traveller;
import com.yh.service.MemberService;
import com.yh.service.OrdersService;
import com.yh.service.ProductService;
import com.yh.service.TravellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.security.RolesAllowed;
import java.util.List;

@Controller
@RequestMapping("order")
public class OrdersController {
    @Autowired
    OrdersService ordersService;

    @Autowired
    ProductService productService;

    @Autowired
    MemberService memberService;

    @Autowired
    TravellerService travellerService;

    //方法级别的权限控制
    //@RolesAllowed("ADMIN") //只允许ADMIN角色访问  ROLE_ADMIN
    @RolesAllowed({"ADMIN","USER",}) //允许ADMIN或USER 角色访问
    //@Secured("ROLE_ADMIN")
    //@PreAuthorize("hasRole('ROLE_ADMIN')") //在方法执行之前 判断是否有权限
    @RequestMapping("findAll")
    public String findAll(@RequestParam(value = "page", defaultValue = "1") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size,
                          Model model) {

        PageHelper.startPage(page, size);
        List<Orders> orderList = ordersService.findAll();
        PageInfo pageInfo = new PageInfo(orderList);

        model.addAttribute("pageInfo", pageInfo);

        return "order-list";//order-list.html
    }

    //    详情 :订单详情( 商品 会员信息 游客信息)
    //    /order/findById?id=25
    @RequestMapping("findById")
    public String findById(@RequestParam("id") Integer id, Model model) {
        Orders order = ordersService.findById(id);
        model.addAttribute("order", order);
        return "order-show";
    }

    //为了跳转到添加页面( 准备数据)
    @RequestMapping("add")
    public String add(Model model) {
        //产品列表
        List<Product> productList = productService.findAll();

        //会员列表
        List<Member> memberList = memberService.findAll();

        //游客列表
        List<Traveller> travellerList = travellerService.findAll();

        model.addAttribute("productList", productList);
        model.addAttribute("memberList", memberList);
        model.addAttribute("travellerList", travellerList);
        return "order-add";
    }

    ///order/save
    @PostMapping("save")
    public String save(Orders order) {//  pid mid  tids: 1,2,5
        //新增订单基本信息
        ordersService.save(order);

        //  新增订单和游客的关系
        ordersService.saveOrderAndTravellerRelation(order.getId(), order.getTravellerId());

        return "redirect:findAll";
    }

    //修改之前的数据回显
    //order/editById?id=25
    @RequestMapping("editById")
    public String editById(@RequestParam("id") Integer id, Model model) {
        Orders order = ordersService.findById(id);
        model.addAttribute("order", order);
        return "order-edit";
    }

    // 修改操作   edit
    @RequestMapping("edit")
    public String edit(Orders order) {
        ordersService.edit(order);
        return "redirect:findAll";
    }


    // /deleteById?id=25
    @RequestMapping("deleteById")
    public String deleteById(@RequestParam("id") Integer id) {
        ordersService.deleteById(id);
        return "redirect:findAll";
    }

    //批量删除
    //order/selectDelete
    @RequestMapping("selectDelete")
    public String selectDelete(Integer[] ids) {
        ordersService.selectDelete(ids);
        return "redirect:findAll";
    }


}
